package com.ohuang.imghide.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.icu.util.Measure;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class FillImageView extends androidx.appcompat.widget.AppCompatImageView {
    public static final String TAG = FillImageView.class.getSimpleName();
    private int height;
    public FillImageView(@NonNull Context context) {
        super(context);
    }

    public FillImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public FillImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (height>200) {
            int h=MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
            super.onMeasure(widthMeasureSpec, h);
        }else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    public void setImageBitmap(Bitmap bm) {

        height=(bm.getHeight()*getWidth()/ bm.getWidth());
        Log.d(TAG, "setImageBitmap: "+height);
        super.setImageBitmap(bm);
        requestLayout();
    }
}
